home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4584 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: ix.netcom.com!netnews
  2. From: judgemi@ix.netcom.com (Michael Judge )
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why Do I Use An Ampersand in Member Class Parameters?
  5. Date: 31 Jan 1996 04:06:34 GMT
  6. Organization: Netcom
  7. Message-ID: <4empsa$18b@ixnews5.ix.netcom.com>
  8. References: <4emnv2$n5o@alcor.usc.edu>
  9. NNTP-Posting-Host: ix-or11-13.ix.netcom.com
  10. X-NETCOM-Date: Tue Jan 30  8:06:34 PM PST 1996
  11.  
  12. In <4emnv2$n5o@alcor.usc.edu> wawda@alcor.usc.edu (Abu Wawda) writes: 
  13. >
  14. >class Simple
  15. >{
  16. >public:
  17. >  Simple();
  18. >  int operator += (const Simple &);
  19. >private:
  20. >  int data;
  21. >};
  22. >
  23. >I have seen many examples of this but I what I don't understand is why
  24. >there is an amersand after Simple? I would wind up implementating the
  25. >function as something like the following:
  26. >
  27. >int Simple::operator += (const Simple ¶meter)
  28. >{
  29. >  data += parameter.data;
  30. >}
  31. >
  32. >I mean, I would understand if I parameter were a pointer to a Simple
  33. >class, but that is not the case, since I am not doing:
  34. >
  35. >int Simple::operator += (const Simple ¶meter)
  36. >{
  37. >  data += parameter->data;
  38. >}
  39. >
  40. >Then why is there an amersand? I would appreciate any
  41. >suggestions. Thank you!
  42. >
  43. >
  44. >-Abu Wawda
  45. > wawda@scf.usc.edu
  46. >
  47. >
  48.  
  49. I am not sure i understand this, but the & makes the parameter a
  50. reference. That way a copy of simple is not made when the method is
  51. used. Use & insted of * because you are more assured that the parameter
  52. is a legal object. (unless someone did some evil casting)
  53.  
  54. note: i dont have my compiler with me but i dont think one simple
  55. object can necessarily access another's private members without an
  56. access method like int Simple::getData()const; If i am mistaken someone
  57. please correct me.
  58.